package com.etc.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.UUID;

/**
 *
 */
public class CommonUtil {
    public static void main(String[] args) {
        
    }

    public static byte[] getByteArrya(Object object) throws UtilException {

        if (object != null) {
            String base64Data = String.valueOf(object);
            return changeImage(base64Data);
        }
        return null;
    }
    private static byte[] changeImage(String base64Data) throws UtilException {
        byte[] b = null;
        String dataPrix = ""; //base64格式前头
        String data = "";//实体部分数据
        String [] d = base64Data.split("base64,");//将字符串分成数组
        if(d != null && d.length == 2){
            dataPrix = d[0];
            data = d[1];
        }else {
            throw new UtilException("上传失败，数据不合法");
        }
        String suffix = "";//图片后缀，用以识别哪种格式数据
        //编码的jpeg图片数据
        if("data:image/jpeg;".equalsIgnoreCase(dataPrix)){
            suffix = ".jpg";
        }else if("data:image/x-icon;".equalsIgnoreCase(dataPrix)){
            //编码的icon图片数据
            suffix = ".ico";
        }else if("data:image/gif;".equalsIgnoreCase(dataPrix)){
            //编码的gif图片数据
            suffix = ".gif";
        }else if("data:image/png;".equalsIgnoreCase(dataPrix)){
            //编码的png图片数据
            suffix = ".png";
        }else {
            throw new UtilException("上传图片格式不合法");
        }
        Decoder decoder = Base64.getDecoder();
        //Base64解码
        b = decoder.decode(data);
        return b;
    
    }

    public String uploadImage(String base64Data){
        String dataPrix = ""; //base64格式前头
        String data = "";//实体部分数据
        if(base64Data==null||"".equals(base64Data)){
            return "上传失败，上传图片数据为空";
        }else {
            String [] d = base64Data.split("base64,");//将字符串分成数组
            if(d != null && d.length == 2){
                dataPrix = d[0];
                data = d[1];
            }else {
                return "上传失败，数据不合法";
            }
        }
        String suffix = "";//图片后缀，用以识别哪种格式数据
        //编码的jpeg图片数据
        if("data:image/jpeg;".equalsIgnoreCase(dataPrix)){
            suffix = ".jpg";
        }else if("data:image/x-icon;".equalsIgnoreCase(dataPrix)){
            //编码的icon图片数据
            suffix = ".ico";
        }else if("data:image/gif;".equalsIgnoreCase(dataPrix)){
            //编码的gif图片数据
            suffix = ".gif";
        }else if("data:image/png;".equalsIgnoreCase(dataPrix)){
            //编码的png图片数据
            suffix = ".png";
        }else {
            return "上传图片格式不合法";
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String tempFileName=uuid+suffix;
        String imgFilePath = "G:\\Images\\"+tempFileName;//新生成的图片
        Decoder decoder = Base64.getDecoder();
        try {
            //Base64解码
            byte[] b = decoder.decode(data);
            for(int i=0;i<b.length;++i) {
                if(b[i]<0) {
                    //调整异常数据
                    b[i]+=256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            String imgurl="http://xxxxxxxx/"+tempFileName;
            //imageService.save(imgurl);
            return imgurl;
        } catch (IOException e) {
            e.printStackTrace();
            return "上传图片失败";
        }
    
    }
}

